package de.ruedigermoeller.reallive.facade.database;

import de.ruedigermoeller.heapoff.structs.FSTStruct;
import de.ruedigermoeller.reallive.facade.collection.RLRow;
import de.ruedigermoeller.reallive.facade.collection.TableName;

import java.io.Serializable;

/**
 * Copyright (c) 2012, Ruediger Moeller. All rights reserved.
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * <p/>
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 * <p/>
 * Date: 03.11.13
 * Time: 19:36
 * To change this template use File | Settings | File Templates.
 */
public class RLTableConfiguration<T extends RLRow> implements Serializable {

    int tableId;
    Class<T> rowClass;
    private T template;
    String tableName;
    String description;

    public RLTableConfiguration() {
    }

    public RLTableConfiguration(int tableId, T template) {
        this.tableId = tableId;
        this.rowClass = (Class<T>) template.getClass();
        this.template = (T) ((FSTStruct)template).toOffHeap();
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTableName() {
        if ( tableName == null ) {
            TableName name = rowClass.getAnnotation(TableName.class);
            if ( name != null )
                return name.value();
            return rowClass.getSimpleName();
        }
        return tableName;
    }

    public int getTableId() {
        return tableId;
    }

    public void setTableId(int tableId) {
        this.tableId = tableId;
    }

    public Class<T> getRowClass() {
        return rowClass;
    }

    public void setRowClass(Class<T> rowClass) {
        this.rowClass = rowClass;
    }

    public T getTemplate() {
        return template;
    }

    public void setTemplate(T template) {
        this.template = template;
    }

    @Override
    public String toString() {
        return "RLTableConfiguration{" +
                "tableId=" + tableId +
                ", rowClass=" + rowClass +
                ", template=" + template +
                '}';
    }
}
